﻿using ERP.ERPSystem.HumanResources.Dto;
using ERP.Team.HumanResources;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Users;

namespace ERP.ERPSystem.HumanResources
{
    /// <summary>
    /// 人资系统-入职审核
    /// </summary>
    public class OnboardingReviewServices: ApplicationService, IOnboardingReviewServices
    {
        /// <summary>
        /// 入职审核仓储
        /// </summary>
        private readonly IRepository<OnboardingReview> _onboardingreviewRepsoitory;
        /// <summary>
        /// 构造函数，通过依赖注入获取入职审核仓储
        /// </summary>
        /// <param name="onboardingreviewRepsoitory">入职审核仓储实例</param>
        public OnboardingReviewServices(IRepository<OnboardingReview> onboardingreviewRepsoitory)
        {
            _onboardingreviewRepsoitory = onboardingreviewRepsoitory;
        }

        /// <summary>
        /// 创建一条新的入职审核记录
        /// </summary>
        /// <param name="onboardingReviewDto">包含新审核信息的DTO</param>
        /// <returns>返回新创建记录的ID</returns>
        public async Task<int> AddAuditOnboardingReview(OnboardingReviewDto onboardingReviewDto)
        {
            // 1. 使用 AutoMapper 将 DTO 映射到实体
            var onboardingReviewEntity = ObjectMapper.Map<OnboardingReviewDto, OnboardingReview>(onboardingReviewDto);
            // 2. 将新实体插入到数据库
            var newEntity = await _onboardingreviewRepsoitory.InsertAsync(onboardingReviewEntity);
            // 3. 返回新创建实体的ID
            return newEntity.Id;
        }

        /// <summary>
        /// 根据条件查询审核列表 (非分页)
        /// </summary>
        /// <param name="personName">(筛选条件) 申请人姓名</param>
        /// <param name="interviewPeople">(筛选条件) 面试官姓名</param>
        /// <returns>返回符合条件的入职审核实体列表</returns>
        public async Task<List<OnboardingReview>> ShowAuditOnboardingReview(string? personName, string? interviewPeople)
        {
            try
            {
                // 1. 获取 IQueryable 对象以构建动态查询
                var queryable = await _onboardingreviewRepsoitory.GetQueryableAsync();

                // 2. 应用筛选条件
                var query = queryable
                    .WhereIf(!string.IsNullOrWhiteSpace(personName), x => x.PersonName.Contains(personName))
                    .WhereIf(!string.IsNullOrWhiteSpace(interviewPeople), x => x.InterviewPeople.Contains(interviewPeople));

                // 3. 执行查询并返回结果列表
                var result = await AsyncExecuter.ToListAsync(query);
                
                // 4. (可选) 记录日志用于调试
                Logger.LogInformation($"查询条件: personName={personName}, interviewPeople={interviewPeople} | 查询结果数量: {result.Count}");
                
                return result;
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "查询入职审核列表时发生错误");
                throw; // 重新抛出异常，让上层框架处理
            }
        }
    }
}
